$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();	
	
	var name = $("input#name").val();
	if (name == "") {
      $("label#name_error").show();
	  $("input#name").addClass('review-input-error');
      $("input#name").focus();
      return false;
    }
	else if ($("input#name").hasClass('review-input-error'))
	{
		$("input#name").removeClass('review-input-error');
	}

	var email = $("input#email").val();
	if (email == "") {
      $("label#email_error").show();
	  $("input#email").addClass('review-input-error');
      $("input#email").focus();
      return false;
    }
	else if ($("input#email").hasClass('review-input-error'))
	{
		$("input#email").removeClass('review-input-error');
	}

	var subject = $("input#subject").val();
	if (subject == "") {
      $("label#subject_error").show();
	  $("input#subject").addClass('review-input-error');
      $("input#subject").focus();
      return false;
    }
	else if ($("input#subject").hasClass('review-input-error'))
	{
		$("input#subject").removeClass('review-input-error');
	}

	var review = $("#review").val();
	if (review == "" || review.length < 50) {
      $("label#review_error").show();
	  $("#review").addClass('review-input-error');
	  $("#review").focus();
      return false;
    }
	else if ($("#review").hasClass('review-input-error'))
	{
		$("#review").removeClass('review-input-error');
	}


	var rating = $('input[name=rating]:checked').val();
	if (rating != "1" && rating != "2"  && rating != "3"  && rating != "4"  && rating != "5" ) {
      $("label#rating_error").show();
      return false;
    }

	var telephone = $("input#telephone").val();
	var occupation = $("input#occupation").val();
	var products_id = $("input[name=products_id]").val();

		
	var dataString = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&occupation=' + occupation + '&subject=' + subject + '&review=' + review + '&rating=' + rating + '&products_id=' + products_id;
		
		$.ajax({
      type: "POST",
      url: "reviews/bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Review Submitted!</h2>")
        .append("<p>Thank you for providing us an unbiased review. Your review should show up within 72 hours. Thank you for your time and great support.</p>")
        .hide()
        .fadeIn(1000, function() {
          $('#message').append("<img id='checkmark' src='reviews/images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
